//
//  MealTableViewController.m
//  We_Travel
//
//  Created by lanou3g on 15/12/12.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "MealTableViewController.h"
#import "SelectPublicTableViewCell.h"
#import "FirstViewController.h"
#import "WMPageController.h"
#import "NH_IDHelpers.h"
#import "NH_DataDownloadTools.h"
#import "url.h"
#import "NH_AttractionsModel.h"
#import "UIImageView+WebCache.h"
#import "SiteManager.h"
@interface MealTableViewController ()

@property (nonatomic,strong) NSMutableArray *data;

@end

@implementation MealTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"SelectPublicTableViewCell" bundle:nil] forCellReuseIdentifier:@"SelectCell_id"];
    [self loadData];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)loadData {
    [NH_DataDownloadTools downloadDataWithURL:[NSString stringWithFormat:NH_AttractUrl,6,[SiteManager shareSiteManager].areaIDCurrent] andMethod:@"get" andBody:nil andBlock:^(NSData *data) {
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSMutableArray *arr = dictionary[@"items"];
        self.data = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            NH_AttractionsModel *model = [[NH_AttractionsModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.data addObject:model];
        }
        [self.tableView reloadData];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete implementation, return the number of rows
    return self.data.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SelectPublicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SelectCell_id"];
    if (!cell) {
        cell = [[SelectPublicTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SelectCell_id"];
    }
    NH_AttractionsModel *model = self.data[indexPath.row];
    cell.recommended_reasonLabel.text = model.recommended_reason;
    cell.nameLabel.text = model.name;
    [cell.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.cover]];
    NSLog(@"%ld~~~~~%ld",model.time_consuming_max,model.time_consuming_min);
    cell.time_consuming_min.text = [NSString stringWithFormat:@"%ld%ld",model.time_consuming_min,model.time_consuming_max];
    if ([cell.time_consuming_min.text isEqualToString:@"00"]) {
        cell.time_consuming_min.text = [NSString stringWithFormat:@""];
    }else if ([cell.time_consuming_min.text isEqualToString:@"3030"]) {
        cell.time_consuming_min.text = [NSString stringWithFormat:@"耗时:30分钟"];
    }else if ([cell.time_consuming_min.text isEqualToString:@"6060"]) {
        cell.time_consuming_min.text = [NSString stringWithFormat:@"耗时:约1小时"];
    }else {
        cell.time_consuming_min.text = [NSString stringWithFormat:@"耗时:%ld-%ld小时",model.time_consuming_min / 60,model.time_consuming_max / 60];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 250;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
